package com.example.sunmiv1.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.adapter.WarehousingDetailAdapter;
import com.example.sunmiv1.entity.WarehousingDetailInfo;
import com.example.sunmiv1.utils.LogUtil;
import com.example.sunmiv1.utils.Tools;
import com.example.sunmiv1.utils.net.ACache;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.net.GsonUtils;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.LoadingDialog;
import com.example.sunmiv1.utils.net.TextCallBack;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class WarehousingDetailActivity extends Activity {

    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_title)
    TextView titleTitle;
    @BindView(R.id.remark_warehousingDetail)
    TextView remarkWarehousingDetail;
    @BindView(R.id.listView_warehousingDetail)
    ListView listViewWarehousingDetail;
    @BindView(R.id.ll_remark_warehousingDetail)
    LinearLayout llRemarkWarehousingDetail;

    private Intent intent;
    private Context context;
    private ACache aCache;
    private LoadingDialog loadingDialog;
    private WarehousingDetailInfo warehousingDetailInfo;
    private List<WarehousingDetailInfo.DataBean> list = new ArrayList<>();
    private WarehousingDetailAdapter warehousingDetailAdapter;

    private String status;// "1"爲調撥詳情  “2”爲入庫詳情

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    setWarehousingDetailAdapter();
                    break;
                case 2:
                    setWarehousingDetailAdapter();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_warehousing_detail);
        ButterKnife.bind(this);
        intent = getIntent();
        status = intent.getStringExtra("status");
        LogUtil.i("WarehousingDetailActivity.status:" + status);
        if ("1".equals(status)) {
            titleTitle.setText(R.string.goodWarehousing_detail);
        } else {
            titleTitle.setText(R.string.WarehousingDetails);
        }
        Tools.hideViewInItem(intent.getStringExtra("remark"), remarkWarehousingDetail);
        context = this;
        aCache = ACache.get(context);
        loadingDialog = new LoadingDialog(context);
        if ("1".equals(status)) {
            getAlloDetail();
        } else {
            getWarehousingDetail();
        }
    }

    @OnClick(R.id.title_back)
    public void onViewClicked() {
        finish();
    }

    private void setWarehousingDetailAdapter() {
        if (warehousingDetailAdapter == null) {
            warehousingDetailAdapter = new WarehousingDetailAdapter(context, list, status);
            listViewWarehousingDetail.setAdapter(warehousingDetailAdapter);
        } else {
            warehousingDetailAdapter.notifyDataSetChanged();
        }
        warehousingDetailAdapter.setType(intent.getStringExtra("type"));
    }

    // 調撥詳情
    private void getAlloDetail() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("merchantid", aCache.getAsString("merchantid"));
            jsonObject.put("storeid", aCache.getAsString("storeid"));
            jsonObject.put("id", intent.getStringExtra("id"));
            jsonObject.put("type", intent.getStringExtra("type"));
            jsonObject.put("user_type", aCache.getAsString("userType"));
            if ("1".equals(aCache.getAsString("userType")) || "2".equals(aCache.getAsString("userType"))) {
                jsonObject.put("operator_id", aCache.getAsString("operator_id"));
            } else {
                jsonObject.put("operator_id", aCache.getAsString("uid"));
            }
            HttpUtils.post(context, Common.GET_ALLOCATE_DETAILS, jsonObject, new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    warehousingDetailInfo = GsonUtils.JsonToBean(text, WarehousingDetailInfo.class);
                    Message msg = new Message();
                    if (warehousingDetailInfo.getStatus() == 1) {
                        list.addAll(warehousingDetailInfo.getData());
                        msg.what = 1;
                    } else {
                        msg.what = 2;
                    }
                    handler.sendMessage(msg);
                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    // 入庫詳情
    private void getWarehousingDetail() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("merchantid", aCache.getAsString("merchantid"));
            jsonObject.put("storeid", aCache.getAsString("storeid"));
            jsonObject.put("id", intent.getStringExtra("id"));
            if ("1".equals(aCache.getAsString("userType")) || "2".equals(aCache.getAsString("userType"))) {
                jsonObject.put("operator_id", aCache.getAsString("operator_id"));
            } else {
                jsonObject.put("operator_id", aCache.getAsString("uid"));
            }
            jsonObject.put("user_type", aCache.getAsString("userType"));
            HttpUtils.post(context, Common.GET_ENTER_DETAILS, jsonObject, new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    warehousingDetailInfo = GsonUtils.JsonToBean(text, WarehousingDetailInfo.class);
                    Message msg = new Message();
                    if (warehousingDetailInfo.getStatus() == 1) {
                        list.addAll(warehousingDetailInfo.getData());
                        msg.what = 1;
                    } else {
                        msg.what = 2;
                    }
                    handler.sendMessage(msg);
                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
